﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class LayFireHydrantForms : System.Windows.Forms.Form
	{
		public LayFireHydrantForms()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public LayFireHydrantForms(TreeView tv, Autodesk.Revit.DB.Document doc, string typeName)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.Text = typeName + "布置";
			if (typeName == "消火栓")
			{
				this.userSetName = "firehydrant";
			}
			else if (typeName == "水泵")
			{
				this.userSetName = "waterpump";
			}
			else
			{
				this.userSetName = "watertanks";
			}
			this.plumbDR = new PlumbingDataReader(doc);
			this.initParameterSetting();
			this.treeView = tv;
			base.Controls.Add(this.treeView);
			this.initTreeView();
			this.doc = doc;
			this.initFamilyTypeCB();
		}

		private void initParameterSetting()
		{
			this.hyTbOffSet.Text = this.userSetting.GetValue(this.userSetName, "offset");
			try
			{
				this.cbTag.Checked = Convert.ToBoolean(this.userSetting.GetValue(this.userSetName, "tag"));
			}
			catch (Exception)
			{
			}
		}

		private void initTreeView()
		{
			this.treeView.Location = new System.Drawing.Point(14, 16);
			this.treeView.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.treeView.Width = 200;
			this.treeView.Height = 242;
			this.treeView.NodeMouseClick += this.treeView_NodeMouseClick;
		}

		private void initFamilyTypeCB()
		{
			this.name = this.userSetting.GetValue("laysetting", this.userSetName).Split(new char[]
			{
				'|'
			});
			if (this.name.Count<string>() > 0)
			{
				TreeNode treeNode = this.GetUserFamilyNode(this.name[0]);
				if (treeNode != null)
				{
					this.treeView.SelectedNode = treeNode;
				}
				else if (this.treeView.Nodes != null && this.treeView.Nodes.Count > 0)
				{
					treeNode = TreeViewTools.GetFirstNode(this.treeView.Nodes[0]);
					this.treeView.SelectedNode = treeNode;
				}
				this.DisPlayImgAndParameter(treeNode);
			}
			if (this.name.Count<string>() > 1)
			{
				this.lsbFamilyType.SelectedItem = this.name[1];
			}
		}

		private TreeNode GetUserFamilyNode(string name)
		{
			TreeNode treeNode = null;
			foreach (object obj in this.treeView.Nodes)
			{
				treeNode = TreeViewTools.GetSelectedNode((TreeNode)obj, name);
				if (treeNode != null)
				{
					break;
				}
			}
			return treeNode;
		}

		protected void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.DisPlayImgAndParameter(e.Node);
		}

		protected void DisPlayImgAndParameter(TreeNode treeNode)
		{
			try
			{
				if (treeNode != null && treeNode.Tag is DbFamily)
				{
					Image image = (treeNode.Tag as DbFamily).Image;
					this.picBoxpreview.Image = image;
					this.DisPlayFamilyType(treeNode);
					if (this.treeSelectedNode != null)
					{
						this.treeSelectedNode.BackColor = System.Drawing.Color.Transparent;
						this.treeSelectedNode.ForeColor = System.Drawing.Color.Black;
					}
					this.treeSelectedNode = treeNode;
					treeNode.BackColor = System.Drawing.Color.DodgerBlue;
					treeNode.ForeColor = System.Drawing.Color.White;
					if (this.lsbFamilyType.SelectedItem == null && this.lsbFamilyType.Items.Count > 0)
					{
						this.lsbFamilyType.SelectedItem = this.lsbFamilyType.Items[0];
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void DisPlayFamilyType(TreeNode treeNode)
		{
			if (treeNode != null && treeNode.Tag is DbFamily)
			{
				this.lsbFamilyType.Items.Clear();
				List<string> symbols = (treeNode.Tag as DbFamily).Symbols;
				IComparer<string> comparer = new ListSortByNumber();
				symbols.Sort(comparer);
				foreach (string item in symbols)
				{
					this.lsbFamilyType.Items.Add(item);
				}
			}
		}

		private void GetFamilyTypeParameter()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			if (this.lsbFamilyType.SelectedItem != null)
			{
				string typeName = this.lsbFamilyType.SelectedItem.ToString();
				new List<DbParameter>();
				List<DbParameter> symbolParameter = this.plumbDR.GetSymbolParameter(family, typeName);
				this.hyPropertys = new HYPropertys();
				HYProperty[] array = new HYProperty[symbolParameter.Count];
				int num = 0;
				foreach (DbParameter dbParameter in symbolParameter)
				{
					array[num] = new HYProperty();
					if (dbParameter != null && dbParameter.ParamDefinition != null && dbParameter.Value != null)
					{
						array[num].Name = dbParameter.ParamDefinition.Name;
						array[num].Description = dbParameter.ParamDefinition.Name;
						array[num].Value = dbParameter.Value + " " + dbParameter.ParamDefinition.Unit;
						array[num].Category = "参数";
						this.hyPropertys.Add(array[num]);
						num++;
					}
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.hyTbOffSet.Text.Trim() == "")
			{
				MessageBox.Show("参数不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.UserSettingWrite();
			this.initEquipmentCoilParam();
			try
			{
				if (this.lsbFamilyType.SelectedItem != null && this.treeSelectedNode != null && "" != this.lsbFamilyType.SelectedItem.ToString())
				{
					this.FamilyLoad();
					base.DialogResult = DialogResult.OK;
				}
				else
				{
					MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void FamilyLoad()
		{
			DbFamily family = this.treeSelectedNode.Tag as DbFamily;
			string typeName = this.lsbFamilyType.SelectedItem.ToString();
			DbFamilySymbol familySymbol = this.plumbDR.GetFamilySymbol(family, typeName);
			if (familySymbol != null)
			{
				this.fs = familySymbol.Create(this.doc);
				if (this.fs == null)
				{
					throw new Exception("加载族失败");
				}
			}
			else
			{
				MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void UserSettingWrite()
		{
			try
			{
				if (this.treeSelectedNode != null && this.lsbFamilyType.SelectedItem != null && this.lsbFamilyType.SelectedItem.ToString() != "")
				{
					string text = this.treeSelectedNode.Text + "|" + this.lsbFamilyType.SelectedItem.ToString();
					this.userSetting.WriteValue("laysetting", this.userSetName, text);
				}
				this.userSetting.WriteValue(this.userSetName, "offset", this.hyTbOffSet.Text);
				this.userSetting.WriteValue(this.userSetName, "tag", this.cbTag.Checked.ToString());
			}
			catch (Exception)
			{
			}
		}

		private void btnMoreParameter_Click(object sender, EventArgs e)
		{
			if (this.lsbFamilyType.SelectedItem != null && this.lsbFamilyType.SelectedItem.ToString() != "")
			{
				this.GetFamilyTypeParameter();
				new ParameterDisplayForm(this.hyPropertys).ShowDialog();
				return;
			}
			MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}

		private void btnLayArea_Click(object sender, EventArgs e)
		{
			if (this.lsbFamilyType.SelectedItem == null || this.treeSelectedNode == null || "" == this.lsbFamilyType.SelectedItem.ToString())
			{
				MessageBox.Show("请选择族类型", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
		}

		private void initEquipmentCoilParam()
		{
			this.equipmentPara = new EquipmentParam();
			this.equipmentPara.OffSet = Convert.ToDouble(this.hyTbOffSet.Text.Trim());
			this.equipmentPara.Tag = this.cbTag.Checked;
		}

		public FamilySymbol fs;

		private Document doc;

		public EquipmentParam equipmentPara;

		private TreeView treeView = new TreeView();

		private TreeNode treeSelectedNode;

		private PlumbingDataReader plumbDR;

		private HYPropertys hyPropertys;

		private UserSetting userSetting = new UserSetting();

		private string[] name;

		private string userSetName = "firehydrant";
	}
}
